package com.kylin.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kylin.R;
import com.kylin.eduservice.entity.EduCourse;
import com.kylin.eduservice.entity.vo.CourseInfoVo;
import com.kylin.eduservice.entity.vo.CoursePublishVo;
import com.kylin.eduservice.entity.vo.CourseQuery;
import com.kylin.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author kylin
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    EduCourseService eduCourseService;


    //获取课程基本信息
    @GetMapping()
    @ApiOperation("获取课程基本信息")
    public R getCourseList(){

        List<EduCourse> list = eduCourseService.list(null);

        return R.ok().data("list",list);
    }

    //分页查询课程基本信息
    @PostMapping("getCourseListPage/{current}/{limit}")
    @ApiOperation("获取课程分页基本信息")
    public R getCourseListPage(
            @PathVariable("current") long current,
            @PathVariable("limit") long limit,
            @RequestBody CourseQuery courseQuery){

        //System.out.println(courseQuery);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();

        Page<EduCourse> coursePage = new Page<>(current, limit);

        if (title!=null){
            //模糊查询
            wrapper.like("title",title);
        }
        if (status!=null){
            //等于
            wrapper.eq("status",status);
        }
        if (begin!=null){
            //大于等于
            wrapper.ge("gmt_create",begin);
        }
        if (end!=null){
            //小于等于
            wrapper.le("gmt_modified",end);
        }
        //按创建时间排序降序
        wrapper.orderByDesc("gmt_create");

        eduCourseService.page(coursePage,wrapper);
        //获取总记录数
        long total = coursePage.getTotal();
        //获取list集合
        List<EduCourse> records = coursePage.getRecords();

        return R.ok().data("rows",records).data("total",total);
    }

    //添加课程基本信息
    @PostMapping("/addCourseInfo")
    @ApiOperation("添加课程基本信息")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {

        String id = eduCourseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId", id);
    }

    //根据课程Id查询课程基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    @ApiOperation("根据课程Id查询课程基本信息")
    public R getCourseInfo(@PathVariable String courseId) {

        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);

        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    @ApiOperation("修改课程信息")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {

        eduCourseService.updateCourseInfo(courseInfoVo);

        return R.ok();
    }

    //根据课程id查询课程确认信息 存在bug只能返回一条数据
    @GetMapping("/getPublishCourseInfo/{id}")
    @ApiOperation("根据课程Id查询课程确认信息")
    public R getPublishCourseInfo(@PathVariable("id") String id ){

        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布，修改课程状态 Draft Normal
    @PostMapping("/publishCourse/{id}")
    @ApiOperation("课程最终发布,修改课程状态")
    public R publishCourse(@PathVariable("id") String id){

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//课程状态
        eduCourseService.updateById(eduCourse);

        return R.ok();
    }

    //删除课程
    @DeleteMapping("/{courseId}")
    @ApiOperation("删除课程")
    private R deleteCourse(@PathVariable("courseId") String courseId){

        eduCourseService.removeCourse(courseId);

        return R.ok();
    }

}

